package app.sb.jly.com.ji_liang_yuan.activitys.ac;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import app.sb.jly.com.ji_liang_yuan.R;
import app.sb.jly.com.ji_liang_yuan.activitys.application.BaseApplication;
import app.sb.jly.com.ji_liang_yuan.activitys.commons.Constants;
import app.sb.jly.com.ji_liang_yuan.activitys.model.CheckDataModel;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectActivity extends BaseActivity {


    @BindView(R.id.ac_select_rb_duozhou)
    RadioButton acSelectRbDuozhou;
    @BindView(R.id.ac_select_rb_jiaojie)
    RadioButton acSelectRbJiaojie;
    @BindView(R.id.ac_select_rb_both)
    RadioButton acSelectRbBoth;
    @BindView(R.id.ac_select_group1)
    RadioGroup acSelectGroup1;
    @BindView(R.id.ac_select_rb_3)
    RadioButton acSelectRb3;
    @BindView(R.id.ac_select_rb_4)
    RadioButton acSelectRb4;
    @BindView(R.id.ac_select_rb_5)
    RadioButton acSelectRb5;
    @BindView(R.id.ac_select_rb_6)
    RadioButton acSelectRb6;
    @BindView(R.id.ac_select_group2)
    RadioGroup acSelectGroup2;
    @BindView(R.id.ac_select_et_result)
    EditText acSelectEtResult;
    @BindView(R.id.ac_select_layout_single)
    LinearLayout acSelectLayoutSingle;
    @BindView(R.id.ac_select_tv_zhoushu_gangxing)
    TextView acSelectTvZhoushuGangxing;
    @BindView(R.id.ac_select_rb_gx_3)
    RadioButton acSelectRbGx3;
    @BindView(R.id.ac_select_rb_gx_4)
    RadioButton acSelectRbGx4;
    @BindView(R.id.ac_select_rb_gx_5)
    RadioButton acSelectRbGx5;
    @BindView(R.id.ac_select_group3)
    RadioGroup acSelectGroup3;
    @BindView(R.id.ac_select_tv_zhoushu_jiaojie)
    TextView acSelectTvZhoushuJiaojie;
    @BindView(R.id.ac_select_rb_jj_4)
    RadioButton acSelectRbJj4;
    @BindView(R.id.ac_select_rb_jj_5)
    RadioButton acSelectRbJj5;
    @BindView(R.id.ac_select_rb_jj_6)
    RadioButton acSelectRbJj6;
    @BindView(R.id.ac_select_group4)
    RadioGroup acSelectGroup4;
    @BindView(R.id.ac_select_et_result_gangxing)
    EditText acSelectEtResultGangxing;
    @BindView(R.id.ac_select_et_result_jiaojie)
    EditText acSelectEtResultJiaojie;
    @BindView(R.id.ac_select_layout_double)
    LinearLayout acSelectLayoutDouble;
    @BindView(R.id.ac_select_btn_finish)
    Button acSelectBtnFinish;
    private String carModel = "", carModelA = "", carModelB = "";//刚性-A 铰接-B
    private int zhoushu = 0, zhoushuA = 0, zhoushuB = 0;
    private int result = 0, resultA = 0, resultB = 0;
    private String zhoushuText = "", zhoushuAText = "", zhoushuBText = "";
    public LocationClient mLocationClient = null;
    private MyLocationListener myListener = new MyLocationListener();
    boolean isFirst = false;
    int in_type = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);
        ButterKnife.bind(this);
        setTopTitle("主界面");
        setBackBtn();
        carModel = "";
        zhoushu = 0;
        result = 0;
        acSelectGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.ac_select_rb_duozhou:
                        carModel = "多轴刚性";
                        in_type = 1;
                        acSelectLayoutSingle.setVisibility(View.VISIBLE);
                        acSelectLayoutDouble.setVisibility(View.GONE);
                        acSelectEtResult.setText("31000");
                        carModelA = "";
                        carModelB = "";
                        zhoushuA = 0;
                        zhoushuB = 0;
                        resultA = 0;
                        resultB = 0;
                        break;
                    case R.id.ac_select_rb_jiaojie:
                        carModel = "铰接挂车";
                        in_type = 2;
                        acSelectLayoutSingle.setVisibility(View.VISIBLE);
                        acSelectLayoutDouble.setVisibility(View.GONE);
                        acSelectEtResult.setText("40500");
                        carModelA = "";
                        carModelB = "";
                        zhoushuA = 0;
                        zhoushuB = 0;
                        resultA = 0;
                        resultB = 0;
                        break;
                    case R.id.ac_select_rb_both:
                        carModel = "多轴刚性和铰接挂车";
                        carModelA = "刚性";
                        carModelB = "铰接挂车";
                        in_type = 3;
                        acSelectLayoutSingle.setVisibility(View.GONE);
                        acSelectLayoutDouble.setVisibility(View.VISIBLE);
                        carModel = "";
                        zhoushu = 0;
                        result = 0;
                        break;
                }
            }
        });
        acSelectGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.ac_select_rb_3:
                        zhoushu = 3;
                        zhoushuText = "三轴";
                        break;
                    case R.id.ac_select_rb_4:
                        zhoushu = 4;
                        zhoushuText = "四轴";
                        break;
                    case R.id.ac_select_rb_5:
                        zhoushu = 5;
                        zhoushuText = "五轴";
                        break;
                    case R.id.ac_select_rb_6:
                        zhoushu = 6;
                        zhoushuText = "六轴";
                        break;
                }
            }
        });
        acSelectGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.ac_select_rb_gx_3:
//                        zhoushu = 3;
                        zhoushuA = 3;
                        zhoushuAText = "三轴";
                        break;
                    case R.id.ac_select_rb_gx_4:
//                        zhoushu = 4;
                        zhoushuA = 4;
                        zhoushuAText = "四轴";
                        break;
                    case R.id.ac_select_rb_gx_5:
//                        zhoushu = 5;
                        zhoushuA = 5;
                        zhoushuAText = "五轴";
                        break;
                }
            }
        });
        acSelectGroup4.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.ac_select_rb_jj_4:
//                        zhoushu = 4;
                        zhoushuB = 4;
                        zhoushuBText = "四轴";
                        break;
                    case R.id.ac_select_rb_jj_5:
//                        zhoushu = 5;
                        zhoushuB = 5;
                        zhoushuBText = "五轴";
                        break;
                    case R.id.ac_select_rb_jj_6:
//                        zhoushu = 6;
                        zhoushuB = 6;
                        zhoushuBText = "六轴";
                        break;
                }
            }
        });
        mLocationClient = new LocationClient(getApplicationContext());
        //声明LocationClient类

        mLocationClient.registerLocationListener(myListener);
        //注册监听函数
        baiduMap();

        mLocationClient.start();


        BaseApplication.myCheckDataModel = new CheckDataModel();

//        acHomeZhizaoDanwei.setText(Constants.zhi_zao_dan_wei[0]);
    }

    void baiduMap() {
        LocationClientOption option = new LocationClientOption();
        option.setIsNeedAddress(true);
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
//可选，设置定位模式，默认高精度
//LocationMode.Hight_Accuracy：高精度；
//LocationMode. Battery_Saving：低功耗；
//LocationMode. Device_Sensors：仅使用设备；
//LocationMode.Fuzzy_Locating, 模糊定位模式；v9.2.8版本开始支持，可以降低API的调用频率，但同时也会降低定位精度；

        option.setCoorType("bd09ll");
//可选，设置返回经纬度坐标类型，默认GCJ02
//GCJ02：国测局坐标；
//BD09ll：百度经纬度坐标；
//BD09：百度墨卡托坐标；
//海外地区定位，无需设置坐标类型，统一返回WGS84类型坐标

//        option.setFirstLocType(FirstLocType.SPEED_IN_FIRST_LOC);
//可选，首次定位时可以选择定位的返回是准确性优先还是速度优先，默认为速度优先
//可以搭配setOnceLocation(Boolean isOnceLocation)单次定位接口使用，当设置为单次定位时，setFirstLocType接口中设置的类型即为单次定位使用的类型
//FirstLocType.SPEED_IN_FIRST_LOC:速度优先，首次定位时会降低定位准确性，提升定位速度；
//FirstLocType.ACCUARACY_IN_FIRST_LOC:准确性优先，首次定位时会降低速度，提升定位准确性；

        option.setScanSpan(1000);
//可选，设置发起定位请求的间隔，int类型，单位ms
//如果设置为0，则代表单次定位，即仅定位一次，默认为0
//如果设置非0，需设置1000ms以上才有效

        option.setOpenGps(true);
//可选，设置是否使用卫星定位，默认false
//使用高精度和仅用设备两种定位模式的，参数必须设置为true

        option.setLocationNotify(true);
//可选，设置是否当卫星定位有效时按照1S/1次频率输出卫星定位结果，默认false

        option.setIgnoreKillProcess(false);
//可选，定位SDK内部是一个service，并放到了独立进程。
//设置是否在stop的时候杀死这个进程，默认（建议）不杀死，即setIgnoreKillProcess(true)

        option.SetIgnoreCacheException(false);
//可选，设置是否收集Crash信息，默认收集，即参数为false

        option.setWifiCacheTimeOut(5 * 60 * 1000);
//可选，V7.2版本新增能力
//如果设置了该接口，首次启动定位时，会先判断当前Wi-Fi是否超出有效期，若超出有效期，会先重新扫描Wi-Fi，然后定位

        option.setEnableSimulateGps(false);
//可选，设置是否需要过滤卫星定位仿真结果，默认需要，即参数为false

        option.setNeedNewVersionRgc(true);
//可选，设置是否需要最新版本的地址信息。默认需要，即参数为true

        mLocationClient.setLocOption(option);
//mLocationClient为第二步初始化过的LocationClient对象
//需将配置好的LocationClientOption对象，通过setLocOption方法传递给LocationClient对象使用
//更多LocationClientOption的配置，请参照类参考中LocationClientOption类的详细说明
    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取经纬度相关（常用）的结果信息
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明

            double latitude = location.getLatitude();    //获取纬度信息
            double longitude = location.getLongitude();    //获取经度信息
            float radius = location.getRadius();    //获取定位精度，默认值为0.0f
//
//            UtilsLog.e("纬度===" + latitude + "");
//            UtilsLog.e("经度===" + longitude + "");

            String coorType = location.getCoorType();
            //获取经纬度坐标类型，以LocationClientOption中设置过的坐标类型为准

            int errorCode = location.getLocType();
            //获取定位类型、定位错误返回码，具体信息可参照类参考中BDLocation类中的说明
//            String addrstr = location.getAddrStr();
//            String Address = location.getAddress().country + "-" + location.getAddress().province
//                    + "-" + location.getAddress().city
//                    + "-" + location.getAddress().district;
//            String myAddress = location.getAddress().address;
//            UtilsLog.e("errorCode==" + errorCode);
////            UtilsLog.e("addrstr==" + addrstr);
////            UtilsLog.e("Address==" + Address);
//            UtilsLog.e("getCountry==" + location.getCountry());
//            UtilsLog.e("getProvince==" + location.getProvince());
//            UtilsLog.e("getCity==" + location.getCity());
//            UtilsLog.e("getDistrict==" + location.getDistrict());
//            UtilsLog.e("getStreet==" + location.getStreet());
//            UtilsLog.e("getStreetNumber==" + location.getStreetNumber());
            if (isFirst == false) {
                if (!TextUtils.isEmpty(location.getCity()) && !TextUtils.isEmpty(location.getDistrict())
                        && !TextUtils.isEmpty(location.getStreet()) && !TextUtils.isEmpty(location.getStreetNumber())
                        ) {
//                    acHomeJiandingDidian.setText(location.getCity() + location.getDistrict() +
//                            location.getStreet() + location.getStreetNumber());
                    BaseApplication.myCheckDataModel.setJiandingdidian(location.getCity() + location.getDistrict() +
                            location.getStreet() + location.getStreetNumber());
                    isFirst = true;
                }
            }
        }
    }

    @OnClick(R.id.ac_select_btn_finish)
    public void onViewClicked() {
        result = Integer.parseInt(acSelectEtResult.getText().toString());
        resultA = Integer.parseInt(acSelectEtResultGangxing.getText().toString().trim());
        resultB = Integer.parseInt(acSelectEtResultJiaojie.getText().toString().trim());
        if (in_type == 1) {
            //多轴刚性
            if (carModel.length() > 0 && zhoushu != 0 && result != 0) {
                Intent intent = new Intent();
                intent.putExtra("carModel", carModel);
                intent.putExtra("zhoushu", zhoushu);
                intent.putExtra("result", result);
                intent.putExtra("in_type", in_type);
                intent.putExtra("zhoushuText", zhoushuText);
                intent.putExtra("zhizaodanwei", Constants.zhi_zao_dan_wei[0]);
                setResult(RESULT_OK, intent);
                finish();
            }
        } else if (in_type == 2) {
            //铰接挂车
            if (carModel.length() > 0 && zhoushu != 0 && result != 0) {
                Intent intent = new Intent();
                intent.putExtra("carModel", carModel);
                intent.putExtra("zhoushu", zhoushu);
                intent.putExtra("result", result);
                intent.putExtra("in_type", in_type);
                intent.putExtra("zhoushuText",zhoushuText);
                intent.putExtra("zhizaodanwei", Constants.zhi_zao_dan_wei[0]);
                setResult(RESULT_OK, intent);
                finish();
            }
        } else if (in_type == 3) {
            // 多轴刚性和铰接挂车
            carModelA = "刚性";
            carModelB = "铰接挂车";
            if (zhoushuA != 0 && zhoushuB != 0 && resultA != 0 && resultB != 0) {
                Intent intent = new Intent();
                intent.putExtra("carModelA", carModelA);
                intent.putExtra("carModelB", carModelB);
                intent.putExtra("zhoushuA", zhoushuA);
                intent.putExtra("zhoushuB", zhoushuB);
                intent.putExtra("resultA", resultA);
                intent.putExtra("resultB", resultB);
                intent.putExtra("in_type", in_type);
                intent.putExtra("zhoushuAText",zhoushuAText);
                intent.putExtra("zhoushuBText",zhoushuBText);
                intent.putExtra("zhizaodanwei", Constants.zhi_zao_dan_wei[0]);
                setResult(RESULT_OK, intent);
                finish();
            }
        }

    }
}
